package com.impression.chq.home;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.widget.Toast;

import com.ashokvarma.bottomnavigation.BadgeItem;
import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.impression.chq.ui.HomeFragment;

public class MainActivity extends AppCompatActivity {
    private Toolbar toolbar;
    private HomeFragment homeFragment;
    private BottomNavigationBar bottomNavigationBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initUI();
        setDefaultFragment();
        setEvent();
    }

    private void initUI(){
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);    //设置导航栏模式
        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);      //设置导航栏背景模式
        bottomNavigationBar.addItem(new BottomNavigationItem(R.drawable.ic_home_white_24dp,"印象"))
                .addItem(new BottomNavigationItem(R.drawable.ic_book_white_24dp,"日记"))
                .addItem(new BottomNavigationItem(R.drawable.ic_music_note_white_24dp,"分享"))
                .addItem(new BottomNavigationItem(R.drawable.ic_tv_white_24dp,"视频"));
        bottomNavigationBar.setActiveColor(R.color.colorPrimary);       //设置按钮点击颜色(默认主题的PrimaryColor)
                //bottomNavigationBar.setInActiveColor("#FFFFFF")        //设置按钮默认颜色(默认是Color.LTGRAY灰色)
                //bottomNavigationBar.setBarBackgroundColor("#ECECEC");    //设置在BACKGROUND_STYLE_STATIC 模式下颜色是底部导航栏背景色（默认Color.WHITE）

        //添加按钮上方数字的标记
        BadgeItem numberBadgeItem = new BadgeItem()
                .setBorderWidth(4)
               // .setBackgroundColorResource(R.color.)
                .setText("5")
                .setHideOnSelect(true);
        bottomNavigationBar.addItem(new BottomNavigationItem(R.drawable.ic_videogame_asset_white_24dp,"我的").setBadgeItem(numberBadgeItem));
        bottomNavigationBar.initialise();
    }

    private void setDefaultFragment(){
        FragmentManager fm = getFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        homeFragment = new HomeFragment();
        transaction.replace(R.id.id_content,homeFragment);
        transaction.commit();
    }

    private void setEvent(){
        bottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
                Toast.makeText(MainActivity.this,""+position,Toast.LENGTH_SHORT).show();
                FragmentManager fm = getFragmentManager();
                //开启Fragment事务
                FragmentTransaction transaction = fm.beginTransaction();
                hideFragments(transaction);
                switch (position){
                    case 0:
                        if(homeFragment==null){
                            homeFragment = new HomeFragment();
                            //使用当前Fragment的布局代替id_content的控件
                            transaction.replace(R.id.id_content,homeFragment);
                        }else {
                            transaction.show(homeFragment);
                        }
                        break;
                    case 1:
                        break;
                    default:
                        break;
                }
                //事务提交
                transaction.commit();
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {

            }
        });
    }

    //隐藏所有的Fragment
    private void hideFragments(FragmentTransaction transaction) {
        if(homeFragment != null) {
            transaction.hide(homeFragment);
        }
    }
}
